<?php
/**
 * IG_Task
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: Task.php 586 2011-05-31 10:07:18Z mmxie $
 */
class IG_Task
{
    const ACTIVE_PAGE_VIEW = 101;
    const ACTIVE_AWARD = 107;

    const PRODUCT_ACTOR = 102;
    const PRODUCT_USED = 103;
    const PRODUCT_ITEMCARD = 108;
    const PRODUCT_PAY = 109;
    const PRODUCT_NOTICE = 110;

    const ADVERTISE_USED = 203;

    /**
     * 根据task.xml的配置，实例化任务的处理类
     * @param int $type
     * @param System_Model_Task $mdlTask
     * @param array $params
     * @return IG_Task_Abstract
     * @throws IG_Task_Exception
     */
    public static function factory($type, $mdlTask, $params = array())
    {
        $xml = APPLICATION_PATH . '/configs/task.xml';
        if (!file_exists($xml)) {
            throw new IG_Product_Exception("$xml is not exists");
        }

        $config = new Zend_Config_Xml($xml, null, array('allowModifications' => false));
        $handlerConfig = null;
        if ($config->task->handler) {
            $config = $config->task->handler;
            while ($config->valid()) {
                if ($config->current()->type == $type) {
                    $handlerConfig = $config->current();
                    break;
                }
                $config->next();
            }
        }
        $handlerClass = $handlerConfig->class;
        if ($handlerClass == null) {
            throw new IG_Task_Exception("Can not find handle class for type $type");
        }
        if ($handlerConfig->params) {
            $params = array_merge($params, $handlerConfig->params->toArray());
        }
        if (null !== ($jsonParams = json_decode($mdlTask->getStr3()))) {
            $params = array_merge($params, (array)$jsonParams);
            unset($jsonParams);
        }
        $handler = new $handlerClass($params);
        $handler->i1 = $mdlTask->getI1();
        $handler->i2 = $mdlTask->getI2();
        $handler->i3 = $mdlTask->getI3();
        $handler->i4 = $mdlTask->getI4();
        $handler->str1 = $mdlTask->getStr1();
        $handler->str2 = $mdlTask->getStr2();
        $handler->str3 = $mdlTask->getStr3();
        if ($handlerConfig->plugin) {
            $plugins = $handlerConfig->plugin;
            if ($plugins->instance) {
                $extends = $plugins->instance;
            }

            foreach ($plugins as $key => $value) {
                if ($key != 'instance' && $extends) {
                    if (!class_exists($value)) {
                        throw new IG_Task_Exception("class \"$value\" not exists.");
                    }
                    $plugin = new $value($params);
                    if ($plugin instanceof $extends) {
                        $plugin->i1 = $mdlTask->getI1();
                        $plugin->i2 = $mdlTask->getI2();
                        $plugin->i3 = $mdlTask->getI3();
                        $plugin->i4 = $mdlTask->getI4();
                        $plugin->str1 = $mdlTask->getStr1();
                        $plugin->str2 = $mdlTask->getStr2();
                        $plugin->str3 = $mdlTask->getStr3();
                        $handler->addPlugin($plugin);
                    } else {
                        unset($plugin);
                    }
                }
            }
        }
        return $handler;
    }
}
